home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 October: Mac OS SDK / Dev.CD Oct 96 SDK / Dev.CD Oct 96 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / Draw / Sources / DrawView.cpp < prev    next >
Encoding:
Text File  |  1996-08-16  |  14.6 KB  |  555 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                DrawView.cpp
  4. //    Release Version:    $ ODF 1 $
  5. //
  6. //    Author:                Laurent Delamare
  7. //
  8. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.  
  12. #include "ODFDraw.hpp"
  13.  
  14. #ifndef DRAWVIEW_H
  15. #include "DrawView.h"
  16. #endif
  17.  
  18. #ifndef DEFINES_K
  19. #include "Defines.k"
  20. #endif
  21.  
  22. #ifndef DRAWPART_H
  23. #include "DrawPart.h"
  24. #endif
  25.  
  26. #ifndef DRAWFRM_H
  27. #include "DrawFrm.h"
  28. #endif
  29.  
  30. #ifndef DRAWSEL_H
  31. #include "DrawSel.h"
  32. #endif
  33.  
  34. #ifndef BASESHP_H
  35. #include "BaseShp.h"
  36. #endif
  37.  
  38. #ifndef BOUNDSHP_H
  39. #include "BoundShp.h"
  40. #endif
  41.  
  42. #ifndef LINESHP_H
  43. #include "LineShp.h"
  44. #endif
  45.  
  46. #ifndef OVALSHP_H
  47. #include "OvalShp.h"
  48. #endif
  49.  
  50. #ifndef RECTSHP_H
  51. #include "RectShp.h"
  52. #endif
  53.  
  54. #ifndef RRECTSHP_H
  55. #include "RRectShp.h"
  56. #endif
  57.  
  58. #ifndef TEXTSHP_H
  59. #include "TextShp.h"
  60. #endif
  61.  
  62. #ifndef DRAWPRXY_H
  63. #include "DrawPrxy.h"
  64. #endif
  65.  
  66. #ifndef SHPTRAKR_H
  67. #include "ShpTrakr.h"
  68. #endif
  69.  
  70. #ifndef DRAWCMDS_H
  71. #include "DrawCmds.h"
  72. #endif
  73.  
  74. #ifndef DRAWCLIP_H
  75. #include "DrawClip.h"
  76. #endif
  77.  
  78. #ifndef UTILS_H
  79. #include "Utils.h"
  80. #endif
  81.  
  82. #ifndef DRWDDCMD_H
  83. #include "DrwDDCmd.h"
  84. #endif
  85.  
  86. #ifndef DRAWCONT_H
  87. #include "DrawCont.h"
  88. #endif
  89.  
  90. // ----- Framework Layer -----
  91.  
  92. #ifndef FWUTIL_H
  93. #include "FWUtil.h"
  94. #endif
  95.  
  96. #ifndef FWEVEDEF_H
  97. #include "FWEveDef.h"
  98. #endif
  99.  
  100. #ifndef FWPRESEN_H
  101. #include "FWPresen.h"
  102. #endif
  103.  
  104. #ifndef FWSCROLR_H
  105. #include "FWScrolr.h"
  106. #endif
  107.  
  108. #ifndef FWSCLBAR_H
  109. #include "FWSclBar.h"
  110. #endif
  111.  
  112. #ifndef FWGROWBX_H
  113. #include "FWGrowBx.h"
  114. #endif
  115.  
  116. #ifndef FWCONTXT_H
  117. #include "FWContxt.h"
  118. #endif
  119.  
  120. #ifndef FWCLPCMD_H
  121. #include "FWClpCmd.h"
  122. #endif
  123.  
  124. // ----- OS Layer -----
  125.  
  126. #ifndef FWMENU_H
  127. #include "FWMenu.h"
  128. #endif
  129.  
  130. #ifndef FWRECSHP_H
  131. #include "FWRecShp.h"
  132. #endif
  133.  
  134. #ifndef FWCURSOR_H
  135. #include "FWCursor.h"
  136. #endif
  137.  
  138. #ifndef FWEVENT_H
  139. #include "FWEvent.h"
  140. #endif
  141.  
  142. #ifndef FWODGEOM_H
  143. #include "FWODGeom.h"
  144. #endif
  145.  
  146. #ifndef FWLINSHP_H
  147. #include "FWLinShp.h"
  148. #endif
  149.  
  150. #ifndef FWBMPSHP_H
  151. #include "FWBmpShp.h"
  152. #endif
  153.  
  154. #ifndef SLMixOS_H
  155. #include "SLMixOS.h"
  156. #endif
  157.  
  158. // ----- OpenDoc Includes -----
  159.  
  160. #ifndef SOM_Module_OpenDoc_Commands_defined
  161. #include <CmdDefs.xh>
  162. #endif
  163.  
  164. #ifndef SOM_Module_OpenDoc_StdProps_defined
  165. #include <StdProps.xh>
  166. #endif
  167.  
  168. #ifndef SOM_ODSession_xh
  169. #include <ODSessn.xh>
  170. #endif
  171.  
  172. #ifndef SOM_ODDispatcher_xh
  173. #include <Disptch.xh>
  174. #endif
  175.  
  176. //========================================================================================
  177. // RunTime Info
  178. //========================================================================================
  179.  
  180. #ifdef FW_BUILD_MAC
  181. #pragma segment odfdrawframes
  182. #endif
  183.  
  184. FW_DEFINE_CLASS_M1(CDrawView, FW_CSuperView)
  185.  
  186. const FW_ClassTypeConstant LDrawView = FW_TYPE_CONSTANT('d','r','v','w');
  187. FW_REGISTER_ARCHIVABLE_CLASS(LDrawView, CDrawView, CDrawView::Create, FW_CView::Read, CDrawView::Destroy, FW_CView::Write)
  188.  
  189. //========================================================================================
  190. // CDrawView
  191. //========================================================================================
  192.  
  193. //----------------------------------------------------------------------------------------
  194. // CDrawView::CDrawView
  195. //----------------------------------------------------------------------------------------
  196.  
  197. CDrawView::CDrawView(Environment* ev, CDrawFrame* frame, FW_CRect& bounds, 
  198.                                     FW_CPoint& extent, CDrawPart* drawPart) :
  199.     FW_CSuperView(ev, frame, bounds, kDrawViewID, extent, FW_kXYScrolling),
  200.     fDrawPart(drawPart),
  201.     fDrawFrame(frame),
  202.     fDrawPartContent(drawPart->GetDrawContent())
  203. {    
  204. }
  205.  
  206. //----------------------------------------------------------------------------------------
  207. // CDrawView::CDrawView
  208. //----------------------------------------------------------------------------------------
  209.  
  210. CDrawView::CDrawView(Environment* ev) :
  211.     FW_CSuperView(ev),
  212.     fDrawPart(NULL),
  213.     fDrawFrame(NULL),
  214.     fDrawPartContent(NULL)
  215. {    
  216. }
  217.  
  218. //----------------------------------------------------------------------------------------
  219. // CDrawView::~CDrawView
  220. //----------------------------------------------------------------------------------------
  221.  
  222. CDrawView::~CDrawView()
  223. {
  224. }
  225.  
  226. //----------------------------------------------------------------------------------------
  227. // CDrawView::Draw
  228. //----------------------------------------------------------------------------------------
  229.  
  230. void CDrawView::Draw(Environment *ev, ODFacet* odFacet, ODShape* invalidShape)
  231. {
  232.     // Render inside the content view
  233.     FW_CViewContext vc(ev, this, odFacet, invalidShape);
  234.     FW_CRect invalidRect;
  235.     vc.GetClipRect(invalidRect);
  236.  
  237.     FW_CRectShape::RenderRect(vc, invalidRect, FW_kFill, FW_kWhiteEraseInk);
  238.  
  239.     // ----- Determine what to Render -----
  240.     ODCanvas* canvas = odFacet->GetCanvas(ev);
  241.     FW_Boolean dynamicCanvas = canvas->IsDynamic(ev);
  242.  
  243.     // ----- Render the grid -----
  244.     if (dynamicCanvas)
  245.         DrawGrid(ev, vc, invalidRect);
  246.  
  247.     // ----- Render every shape -----
  248.     FW_CRect updateBox;
  249.     CDrawContentShapeIterator ite(fDrawPartContent);
  250.     for (CBaseShape* theShape = ite.First(); ite.IsNotComplete(); theShape = ite.Next())
  251.     {
  252.         theShape->GetUpdateBox(updateBox);
  253.         if (invalidRect.IsIntersecting(updateBox))
  254.             theShape->RenderShape(ev, odFacet, vc);
  255.     }
  256.  
  257.     // ----- Render selections Handles -----
  258.     FW_CFrame* frame = GetFrame(ev);
  259.     if (frame->HasSelectionFocus(ev) && dynamicCanvas && frame->GetWindow(ev)->IsActive(ev))    // Do not render handles for print / print preview
  260.         fDrawFrame->GetSelection(ev)->RenderSelectionHandles(ev, vc);
  261. }
  262.  
  263. //----------------------------------------------------------------------------------------
  264. // CDrawView::AdjustToNewLayout
  265. //----------------------------------------------------------------------------------------
  266.  
  267. void CDrawView::AdjustToNewLayout(Environment *ev, const FW_CPoint& oldExtent, 
  268.                                     const FW_CPoint& newExtent, FW_Boolean refresh)
  269. {
  270. FW_UNUSED(oldExtent);
  271. FW_UNUSED(newExtent);
  272.  
  273.     fDrawFrame->AdjustContentViewSize(ev, refresh);
  274. }
  275.     
  276. //----------------------------------------------------------------------------------------
  277. // CDrawView::AdjustCursor
  278. //----------------------------------------------------------------------------------------
  279.  
  280. FW_Boolean CDrawView::AdjustCursor(Environment *ev, ODFacet* facet, const FW_CPoint& where)
  281. {
  282.     FW_Boolean cursorAdjusted = FW_CView::AdjustCursor(ev, facet, where);
  283.     
  284.     if (!cursorAdjusted && 
  285.         fDrawPart->GetLastActiveFrame(ev) == (FW_CFrame*)fDrawFrame && 
  286.             fDrawPart->GetTool() != kSelectTool)
  287.     {
  288.         FW_gCrossHairCursor.Select();
  289.         cursorAdjusted = TRUE;
  290.     }
  291.  
  292.     return cursorAdjusted;
  293. }
  294.  
  295. //----------------------------------------------------------------------------------------
  296. //    CDrawView::DoVirtualKey
  297. //----------------------------------------------------------------------------------------
  298.  
  299. FW_Boolean CDrawView::DoVirtualKey(Environment* ev, const FW_CVirtualKeyEvent& theVirtualKeyEvent)
  300. {    
  301.     FW_Boolean wasHandled = FALSE;
  302.     
  303.     switch (theVirtualKeyEvent.GetKeyCode(ev))
  304.     {
  305.         case FW_kVKBackspace:
  306.         case FW_kVKClear:
  307.             if (!fDrawFrame->GetSelection(ev)->IsEmpty(ev))
  308.             {
  309.                 FW_CClipboardCommand* cmd = GetFrame(ev)->NewClipboardCommand(ev, kODCommandClear);
  310.                 if (cmd)
  311.                 {
  312.                     cmd->Execute(ev);
  313.                     wasHandled = TRUE;
  314.                 }
  315.             }
  316.             break;
  317.     }
  318.  
  319.     return wasHandled;
  320. }
  321.  
  322. //----------------------------------------------------------------------------------------
  323. // CDrawView::WantsToBeTarget: 
  324. //----------------------------------------------------------------------------------------
  325.  
  326. FW_Boolean CDrawView::WantsToBeTarget(Environment* ev)
  327. {
  328.     return TRUE;
  329. }
  330.  
  331. //----------------------------------------------------------------------------------------
  332. // CDrawView::DoMouseDown
  333. //----------------------------------------------------------------------------------------
  334.  
  335. FW_Boolean CDrawView::DoMouseDown(Environment *ev, const FW_CMouseEvent& theMouseEvent)
  336. {
  337.     ODFacet*         theFacet = theMouseEvent.GetFacet(ev);
  338.     CDrawSelection*    selection = fDrawFrame->GetSelection(ev);
  339.     
  340.     if (fDrawPart->GetTool() == kSelectTool)
  341.     {
  342.         FW_Boolean handled = FALSE;
  343.         
  344.         if (selection->IsMouseInDraggableItem(ev, fDrawFrame, theMouseEvent, FALSE))
  345.         {
  346.             handled = fDrawFrame->Drag(ev, theMouseEvent);
  347.         }
  348.         else if (!selection->Resize(ev, theMouseEvent))
  349.         {
  350.             selection->SelectWithRectangle(ev, theMouseEvent);
  351.             handled = TRUE;
  352.         }
  353.                             
  354.         return handled;
  355.     }
  356.     
  357.     // ----- If read only don't want to create a new shape -----
  358.     if (fDrawPart->IsReadOnly(ev))
  359.     {
  360.         FW_Beep();
  361.         return TRUE;
  362.     }
  363.     
  364.     // ----- Create a new shape -----
  365.     CBaseShape *theShape =  fDrawPartContent->NewShape(ev, fDrawPart->GetTool() - kLine + kLineShape);
  366.     
  367.     FW_TRY
  368.     {
  369.         FW_CStyle trackStyle(fDrawPart->GetRenderVerb() == kFillOnly ? FW_kFixedPos1 : fDrawPart->GetPenSize(), FW_kGrayPat);
  370.         FW_CInk trackInk(FW_kRGBBlack, FW_kRGBWhite, FW_kXOr);
  371.         theShape->ChangeRenderVerb(ev, fDrawPart, kFrameOnly);
  372.         theShape->SetFrameInk(trackInk);
  373.         theShape->SetFrameStyle(trackStyle);
  374.         
  375.         CShapeTracker tracker(ev, this, theFacet, theShape, fDrawPart->IsAutoGridOn());
  376.         if (tracker.Track(ev, theMouseEvent))
  377.         {
  378.             theShape->SetFrameInk(FW_kNormalInk);
  379.             theShape->SetFrameStyle(FW_kNormalStyle);
  380.             
  381.             FW_CColor color;
  382.     
  383.             theShape->ChangeRenderVerb(ev, fDrawPart, fDrawPart->GetRenderVerb());
  384.             theShape->ChangePenSize(ev, fDrawPart, fDrawPart->GetPenSize());
  385.                     
  386.             fDrawPart->GetFrameColor(color);
  387.             theShape->ChangeFrameColor(ev, fDrawPart, color);
  388.     
  389.             fDrawPart->GetFillColor(color);
  390.             theShape->ChangeFillColor(ev, fDrawPart, color);
  391.     
  392.             theShape->ChangeFramePattern(ev, fDrawPart, fDrawPart->GetFramePattern());
  393.             theShape->ChangeFillPattern(ev, fDrawPart, fDrawPart->GetFillPattern());
  394.             
  395.             CDrawShapeCommand* cmd = FW_NEW(CDrawShapeCommand,
  396.                                             (ev, fDrawPart, fDrawFrame, selection, theShape));
  397.             cmd->Execute(ev);
  398.             
  399.             FW_CRect dragRect;
  400.             theShape->GetDragRect(dragRect);
  401.  
  402.             FW_CAcquiredODShape aqShape = ::FW_NewODShape(ev, dragRect);
  403.             
  404.             CDrawFacetClipper facetClipper(ev, fDrawPart);
  405.             facetClipper.Clip(ev, fDrawFrame->GetPresentation(ev), aqShape);
  406.             
  407.             FW_CViewContext vc(ev, this, theMouseEvent.GetFacet(ev));
  408.             theShape->RenderShape(ev, theFacet, vc);
  409.             
  410.             theShape->GetUpdateBox(ev, aqShape);
  411.             Invalidate(ev, aqShape, TRUE);        // want to invalidate all frames
  412.             
  413.             // ----- Because drawing has already been done in this facet -----
  414.             ViewContentToFrame(ev, aqShape);
  415.             theFacet->Validate(ev, aqShape, NULL);
  416.         }
  417.         else
  418.         {
  419.             delete theShape;
  420.         }
  421.     }
  422.     FW_CATCH_BEGIN
  423.     FW_CATCH_EVERYTHING()
  424.     {
  425.         delete theShape;
  426.         FW_THROW_SAME();
  427.     }
  428.     FW_CATCH_END
  429.     
  430.     return TRUE;
  431. }
  432.  
  433. //----------------------------------------------------------------------------------------
  434. // CDrawView::DrawGrid
  435. //----------------------------------------------------------------------------------------
  436.  
  437. void CDrawView::DrawGrid(Environment *ev, FW_CGraphicContext& gc, const FW_CRect& invalidRect)
  438. {
  439.     if (fDrawFrame->IsGraphicsGridOn())
  440.     {
  441. #if defined(__MWERKS__) && GENERATING68K
  442.         FW_BitPattern vertical;
  443.         vertical.fDataLongs[0] = 0xFF000000;
  444.         vertical.fDataLongs[1] = 0;
  445.         FW_BitPattern horizontal;
  446.         horizontal.fDataLongs[0] = 0x80808080;
  447.         horizontal.fDataLongs[1] = 0x80808080;
  448. #else
  449.         FW_BitPattern vertical = {0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  450.         FW_BitPattern horizontal = {0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80};
  451. #endif        
  452.         FW_CPattern verticalPat(vertical);
  453.         FW_CPattern horizontalPat(horizontal);
  454.         
  455.         FW_Fixed minY = FW_IntToFixed(72 * (FW_FixedToInt(invalidRect.top) / 72));
  456.         FW_Fixed maxY = FW_IntToFixed(72 * (FW_FixedToInt(invalidRect.bottom + FW_kFixed72) / 72));
  457.         FW_Fixed minX = FW_IntToFixed(72 * (FW_FixedToInt(invalidRect.left) / 72));
  458.         FW_Fixed maxX = FW_IntToFixed(72 * (FW_FixedToInt(invalidRect.right + FW_kFixed72) / 72));
  459.  
  460.         // skip top-most and left-most lines
  461.         FW_Fixed startX = (minX == FW_kFixed0) ? FW_kFixed72 : minX;
  462.         FW_Fixed startY = (minY == FW_kFixed0) ? FW_kFixed72 : minY;
  463.         
  464.         FW_CStyle lineStyle(FW_kFixed0, verticalPat);
  465.         FW_CLineShape line(startX, minY, startX, maxY, FW_kNormalInk, lineStyle);
  466.  
  467.         for (FW_Fixed x = startX; x <= maxX; x += FW_kFixed72)
  468.         {
  469.             line.Render(gc);
  470.             line.MoveShape(FW_kFixed72, FW_kFixed0);
  471.         }
  472.         
  473.         line.GetStyle().SetPattern(horizontalPat);
  474.         line.SetLineStart(minX, startY);
  475.         line.SetLineEnd(maxX, startY);
  476.         
  477.         for (FW_Fixed y = startY; y <= maxY; y += FW_kFixed72)
  478.         {
  479.             line.Render(gc);
  480.             line.MoveShape(FW_kFixed0, FW_kFixed72);
  481.         }
  482.     }
  483. }
  484.  
  485. //----------------------------------------------------------------------------------------
  486. // CDrawView::SizeChanged
  487. //----------------------------------------------------------------------------------------
  488. //    My content view size has changed I need to clip my embedded facets. It's better to do it
  489. //    here than in CDrawFrame::FrameShapeChanged because my frame shape can be changing but
  490. //    my bounds may not.
  491.  
  492. void CDrawView::SizeChanged(Environment* ev, const FW_CPoint& oldSize)
  493. {
  494.     FW_CSuperView::SizeChanged(ev, oldSize);
  495.     
  496.     CDrawFacetClipper facetClipper(ev, fDrawPart);
  497.     facetClipper.Clip(ev, fDrawFrame->GetPresentation(ev), NULL);
  498. }
  499.  
  500. //----------------------------------------------------------------------------------------
  501. //    CDrawView::Create
  502. //----------------------------------------------------------------------------------------
  503.  
  504. void* CDrawView::Create(FW_CReadableStream& stream, FW_ClassTypeConstant type)
  505. {
  506. FW_UNUSED(stream);
  507. FW_UNUSED(type);
  508.     FW_SOMEnvironment ev;
  509.     return new CDrawView(ev);
  510. }
  511.  
  512. //----------------------------------------------------------------------------------------
  513. //    CDrawView::Destroy
  514. //----------------------------------------------------------------------------------------
  515.  
  516. void CDrawView::Destroy(void* object, FW_ClassTypeConstant type)
  517. {
  518. FW_UNUSED(type);
  519.     CDrawView* self = (CDrawView*) object;
  520.     delete self;
  521. }
  522.  
  523. //----------------------------------------------------------------------------------------
  524. //    CDrawView::Flatten
  525. //----------------------------------------------------------------------------------------
  526.  
  527. void CDrawView::Flatten(Environment* ev, FW_CWritableStream& archive) const
  528. {
  529.     FW_CSuperView::Flatten(ev, archive);
  530. }
  531.  
  532. //----------------------------------------------------------------------------------------
  533. //    CDrawView::InitializeFromStream
  534. //----------------------------------------------------------------------------------------
  535.  
  536. void CDrawView::InitializeFromStream(Environment* ev, FW_CReadableStream& stream)
  537. {
  538.     FW_CSuperView::InitializeFromStream(ev, stream);
  539.     
  540.     FW_OObjectRegistry*    registry = stream.GetRegistry();
  541.     FW_CFrame* frame = (FW_CFrame*) registry->LookupByID(ev, FW_kPreregisteredFrameObject);
  542.     FW_ASSERT(frame != NULL);
  543.     
  544.     fDrawFrame = FW_DYNAMIC_CAST(CDrawFrame, frame);
  545.     FW_ASSERT(fDrawFrame != NULL);
  546.     
  547.     FW_CEmbeddingPart* part = fDrawFrame->GetPart(ev);
  548.     fDrawPart = FW_DYNAMIC_CAST(CDrawPart, part);
  549.     FW_ASSERT(fDrawPart != NULL);
  550.  
  551.     fDrawPartContent = fDrawPart->GetDrawContent();
  552.     FW_ASSERT(fDrawPartContent != NULL);
  553. }
  554.  
  555.